package jay.home.bet.web.struts;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jay.home.bet.domain.Comp;
import jay.home.bet.domain.Event;
import jay.home.bet.misc.Util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class SubmitEventAction extends BaseAction
{
	protected final Log log = LogFactory.getLog(getClass());
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception
	{		
		Event event = new Event();
		
		request.setCharacterEncoding("utf-8"); 
		
		
		event.getDaxiao().setHome(Util.toDouble(request.getParameter("daxiao.home")));
		event.getDaxiao().setRoad(Util.toDouble(request.getParameter("daxiao.road")));
		event.getDaxiao().setPankou(request.getParameter("daxiao.pankou"));
		
		event.getRangqiu().setHome(Util.toDouble(request.getParameter("rangqiu.home")));
		event.getRangqiu().setRoad(Util.toDouble(request.getParameter("rangqiu.road")));
		
		String who2who = new String(request.getParameter("rangqiu.who2who").getBytes("ISO-8859-1"),"UTF8");
		
		event.getRangqiu().setPankou(request.getParameter("rangqiu.pankou"));
		event.getRangqiu().setWho2who(who2who);
		
		String blog = request.getParameter("blog");
		
		String compId = request.getParameter("compId"); 
		
		if ( compId == null || compId.equals("") )
		{
			
			String homeId = null;
			String roadId = null;
			
			String id = Util.uuid();
				
			event.setId(id);
			
			log.info(id);
			
			event.setType("单式");
			event.setAlive(true);
			event.setPublished(false);
			event.setFrozen(false);
			
			String time = request.getParameter("time");
			homeId = request.getParameter("home");
			roadId = request.getParameter("road");

			event.getComp().setTime(Util.string2Date(time));
			event.getComp().getHome().setId(homeId);
			event.getComp().getRoad().setId(roadId); 
			event.getComp().getLeague().setId(request.getParameter("leagueId"));
					
			Comp comp = getBasicService().getCompByDetail(homeId, roadId, time);
			if ( comp != null && comp.getId() != null && !comp.getId().equals(""))
			{
				request.setAttribute("message", 
									 "主队：" + comp.getHome().getName_zh() + 
									 "\n  客队：" + comp.getRoad().getName_zh() + 
									 "\n  时间：" + time + 
									 "   这场赛事已经存在！");
				return mapping.findForward("error");
			}
				event.getComp().setId(Util.uuid());
				getBasicService().insertComp(event.getComp());
				comp = getBasicService().getComp(event.getComp().getId());
				event.setComp(comp);
			
			event.getDuying().setWin(Util.toDouble(request.getParameter("duying.win")));
			event.getDuying().setLose(Util.toDouble(request.getParameter("duying.lose")));
			event.getDuying().setDraw(Util.toDouble(request.getParameter("duying.draw")));
			
			event.getDanshuang().setOdd(Util.toDouble(request.getParameter("danshuang.odd")));
			event.getDanshuang().setEven(Util.toDouble(request.getParameter("danshuang.even")));
			
			event.getBanquanchang().setHr(Util.toDouble(request.getParameter("banquanchang.hr")));
			event.getBanquanchang().setHh(Util.toDouble(request.getParameter("banquanchang.hh")));
			event.getBanquanchang().setHd(Util.toDouble(request.getParameter("banquanchang.hd")));
			event.getBanquanchang().setRr(Util.toDouble(request.getParameter("banquanchang.rr")));
			event.getBanquanchang().setRh(Util.toDouble(request.getParameter("banquanchang.rh")));
			event.getBanquanchang().setRd(Util.toDouble(request.getParameter("banquanchang.rd")));
			event.getBanquanchang().setDd(Util.toDouble(request.getParameter("banquanchang.dd")));
			event.getBanquanchang().setDh(Util.toDouble(request.getParameter("banquanchang.dh")));
			event.getBanquanchang().setDr(Util.toDouble(request.getParameter("banquanchang.dr")));
			
			event.getBodan().setS00(Util.toDouble(request.getParameter("bodan.s00")));
			event.getBodan().setS01(Util.toDouble(request.getParameter("bodan.s01")));
			event.getBodan().setS02(Util.toDouble(request.getParameter("bodan.s02")));
			event.getBodan().setS03(Util.toDouble(request.getParameter("bodan.s03")));
			event.getBodan().setS04(Util.toDouble(request.getParameter("bodan.s04")));
			event.getBodan().setS10(Util.toDouble(request.getParameter("bodan.s10")));
			event.getBodan().setS11(Util.toDouble(request.getParameter("bodan.s11")));
			event.getBodan().setS12(Util.toDouble(request.getParameter("bodan.s12")));
			event.getBodan().setS13(Util.toDouble(request.getParameter("bodan.s13")));
			event.getBodan().setS14(Util.toDouble(request.getParameter("bodan.s14")));
			event.getBodan().setS20(Util.toDouble(request.getParameter("bodan.s20")));
			event.getBodan().setS21(Util.toDouble(request.getParameter("bodan.s21")));
			event.getBodan().setS22(Util.toDouble(request.getParameter("bodan.s22")));
			event.getBodan().setS23(Util.toDouble(request.getParameter("bodan.s23")));
			event.getBodan().setS24(Util.toDouble(request.getParameter("bodan.s24")));
			event.getBodan().setS30(Util.toDouble(request.getParameter("bodan.s30")));
			event.getBodan().setS31(Util.toDouble(request.getParameter("bodan.s31")));
			event.getBodan().setS32(Util.toDouble(request.getParameter("bodan.s32")));
			event.getBodan().setS33(Util.toDouble(request.getParameter("bodan.s33")));
			event.getBodan().setS34(Util.toDouble(request.getParameter("bodan.s34")));
			event.getBodan().setS40(Util.toDouble(request.getParameter("bodan.s40")));
			event.getBodan().setS41(Util.toDouble(request.getParameter("bodan.s41")));
			event.getBodan().setS42(Util.toDouble(request.getParameter("bodan.s42")));
			event.getBodan().setS43(Util.toDouble(request.getParameter("bodan.s43")));
			event.getBodan().setS44(Util.toDouble(request.getParameter("bodan.s44")));
			event.getBodan().setSxx(Util.toDouble(request.getParameter("bodan.sxx")));
			
			event.getRuqiushu().setS01(Util.toDouble(request.getParameter("ruqiushu.s01")));
			event.getRuqiushu().setS23(Util.toDouble(request.getParameter("ruqiushu.s23")));
			event.getRuqiushu().setS46(Util.toDouble(request.getParameter("ruqiushu.s46")));
			event.getRuqiushu().setSup7(Util.toDouble(request.getParameter("ruqiushu.sup7")));
			
			event.getDaxiaoHalf().setHome(Util.toDouble(request.getParameter("daxiaoHalf.home")));
			event.getDaxiaoHalf().setRoad(Util.toDouble(request.getParameter("daxiaoHalf.road")));
			event.getDaxiaoHalf().setPankou(request.getParameter("daxiaoHalf.pankou"));
			
			event.getRangqiuHalf().setHome(Util.toDouble(request.getParameter("rangqiuHalf.home")));
			event.getRangqiuHalf().setRoad(Util.toDouble(request.getParameter("rangqiuHalf.road")));
			event.getRangqiuHalf().setPankou(request.getParameter("rangqiuHalf.pankou"));

			String who2whoHalf = new String(request.getParameter("rangqiuHalf.who2who").getBytes("ISO-8859-1"),"UTF8");
			event.getRangqiuHalf().setWho2who(who2whoHalf);
			
			event.getDuyingHalf().setWin(Util.toDouble(request.getParameter("duyingHalf.win")));
			event.getDuyingHalf().setLose(Util.toDouble(request.getParameter("duyingHalf.lose")));
			event.getDuyingHalf().setDraw(Util.toDouble(request.getParameter("duyingHalf.draw")));
			
			getOddsService().insertEvent(event);
			
			if( blog != null && !blog.equals("") && blog.equals("on"))
			{
				event.setId(Util.uuid());
				event.setType("滚球");
				getOddsService().insertEvent(event);
			}
			
			
			request.setAttribute("eventCount", 0);
		}
		else 
		{
			event.setType("单式");
			event = getOddsService().insertEvent(event , compId);
			if( blog != null && !blog.equals("") && blog.equals("on"))
			{
				event.setId(Util.uuid());
				event.setType("滚球");
				getOddsService().insertEvent(event);
			}
			int eventCount = getOddsService().getByEvent(compId);
			request.setAttribute("eventCount", eventCount );
		}
		log.info(event.getComp().getId());
		
		
		request.setAttribute("event", event);
		Comp testComp = event.getComp();
		Date testTime = testComp.getTime();
		String time = Util.date2String(testTime);
		
		request.setAttribute("time", time );
		
		return mapping.findForward("SUCCESS");
	}
}
